package com.lizardmind.haofudoctor.ui.aboutMe.editData

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.util.Base64
import android.view.View
import com.blankj.utilcode.util.EncodeUtils
import com.blankj.utilcode.util.EncryptUtils
import com.blankj.utilcode.util.FileIOUtils
import com.blankj.utilcode.util.ImageUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.annotation.GlideModule
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.glide.GlideApp
import com.linxiao.framework.util.ke
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.manager.ImgManager
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.manager.setImageUrl
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.Request
import com.lizardmind.haofudoctor.other.rongYun.DOCTOR_PREFIX
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import io.reactivex.functions.Consumer
import io.rong.imkit.RongIM
import io.rong.imlib.model.UserInfo
import kotlinx.android.synthetic.main.activity_edit_data.*
import kotlinx.android.synthetic.main.include_title_text.view.*
import org.jetbrains.anko.act
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.toast
import java.io.File

/**
 *
 * Created by Extends on 2017/10/30 11:50
 */
class EditDataActivity :BaseActivity() {

    override fun onCreatRootView(): View? {
        setContentView(R.layout.activity_edit_data)
        return null
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        editdata_title.apply {
            imgLeft.setImageResource(R.mipmap.back)
            imgLeft.onClick { finish() }
            titleText.text = "编辑资料"
        }
        edit_data_img.setImageUrl(DoctorInfo.head_img)
        edit_data_layout.onClick {
            PictureSelector.create(this@EditDataActivity)
                    .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()
                    .selectionMode(PictureConfig.SINGLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                    .previewImage(true)// 是否可预览图片 true or false
                    .isCamera(true)// 是否显示拍照按钮 true or false
                    .enableCrop(true)// 是否裁剪 true or false
                    .compress(true)// 是否压缩 true or false
                    .withAspectRatio(1,1)// int 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
                    .circleDimmedLayer(true)// 是否圆形裁剪 true or false
                    .showCropFrame(false)// 是否显示裁剪矩形边框 圆形裁剪时建议设为false   true or false
                    .showCropGrid(false)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false    true or false
                    .cropWH(150,150)
                    .forResult(PictureConfig.CHOOSE_REQUEST)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(resultCode== Activity.RESULT_OK){
            if(requestCode==PictureConfig.CHOOSE_REQUEST){
                val selectList = PictureSelector.obtainMultipleResult(data)
                if(selectList[0].isCompressed){
                    ke(selectList[0].compressPath)
                    val v = ImageUtils.bitmap2Bytes(ImageUtils.getBitmap(selectList[0].compressPath), Bitmap.CompressFormat.PNG)
                    val img = EncodeUtils.base64Encode2String(v)
                    observe {
                        postApi<Request<String>>(hashMapOf("action" to "doc_head_img",
                                "pictures" to img, "token" to DoctorInfo.token),
                                Consumer {
                                    it.dispose {
                                        toast("上传成功")
                                        edit_data_img.setImageUrl(it)
                                        DoctorInfo.head_img = it
                                        RongIM.getInstance().setCurrentUserInfo(UserInfo(DOCTOR_PREFIX +DoctorInfo.id,DoctorInfo.name,
                                                Uri.parse(if(DoctorInfo.head_img.startsWith("http")) DoctorInfo.head_img else {
                                                    ImgManager.IMGURL+DoctorInfo.head_img})))
                                    }
                                },context = this)
                    }
                }else{
                    toast("未知错误，请重新再试")
                }

            }
        }
    }
}